<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Object.defineProperties</title>
</head>
<body>
    <script>
        //要求添加 total 属性, 获得班级的总分数
        var banji = {
            name: 'HTML',
            scores: [
                {
                    name: '张三',
                    score: 90
                },
                {
                    name: '李四',
                    score: 85
                },
                {
                    name: '王五',
                    score: 95
                },
                {
                    name: '赵六',
                    score: 88
                }
            ]
        };

        //为 banji 对象定义一个新的属性
        Object.defineProperties(banji, {
            //总数
            total: {
                //get 方法
                get: function(){
                    //对班级成员的成绩相加
                    var t = 0;
                    //遍历班级成员 this 
                    this.scores.forEach(item => {
                        //成绩相加
                        t += item.score;
                    });
                    //返回 t 变量
                    return t;
                }
            },
            //平均数
            avg: {
                get: function(){
                    return this.total / this.scores.length;
                }
            }
        });

        console.log(banji.total);
        console.log(banji.avg);

        // banji.total = function(){
        //     //遍历 相加
        // }

        // banji.total;
    </script>
</body>

</html>